package com.qianfeng.chinatelcom.apigateway.filters;

//
//                            _ooOoo_  
//                           o8888888o  
//                           88" . "88  
//                           (| -_- |)  
//                            O\ = /O  
//                        ____/`---'\____  
//                      .   ' \\| |// `.  
//                       / \\||| : |||// \  
//                     / _||||| -:- |||||- \  
//                       | | \\\ - /// | |  
//                     | \_| ''\---/'' | |  
//                      \ .-\__ `-` ___/-. /  
//                   ___`. .' /--.--\ `. . __  
//                ."" '< `.___\_<|>_/___.' >'"".  
//               | | : `- \`.;`\ _ /`;.`/ - ` : | |  
//                 \ \ `-. \_ __\ /__ _/ .-` / /  
//         ======`-.____`-.___\_____/___.-`____.-'======  
//                            `=---='  
//  
//         .............................................  
//                  佛祖镇楼                  BUG辟易  
//          佛曰:  
//                  写字楼里写字间，写字间里程序员；  
//                  程序人员写程序，又拿程序换酒钱。  
//                  酒醒只在网上坐，酒醉还来网下眠；  
//                  酒醉酒醒日复日，网上网下年复年。  
//                  但愿老死电脑间，不愿鞠躬老板前；  
//                  奔驰宝马贵者趣，公交自行程序员。  
//                  别人笑我忒疯癫，我笑自己命太贱；  
//  


import com.netflix.zuul.ZuulFilter;
import com.netflix.zuul.context.RequestContext;
import com.netflix.zuul.exception.ZuulException;
import com.qianfeng.chinatelcom.apigateway.feign.CacheService;
import com.qianfeng.smsplatform.common.constants.CacheConstants;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.netflix.zuul.filters.support.FilterConstants;
import org.springframework.stereotype.Component;
import org.springframework.util.DigestUtils;
import org.springframework.util.ObjectUtils;

import javax.servlet.http.HttpServletRequest;
import java.nio.charset.StandardCharsets;
import java.util.Map;

/**
 * Created by Jackiechan on 2021/12/2/上午10:05
 *
 * @author Jackiechan
 * @version 1.0
 * @since 1.0
 */
@Component
public class SignFilter extends ZuulFilter {

    private CacheService cacheService;

    @Autowired
    public void setCacheService(CacheService cacheService) {
        this.cacheService = cacheService;
    }

    @Override
    public String filterType() {
        return FilterConstants.PRE_TYPE;
    }

    @Override
    public int filterOrder() {
        return FilterConstants.PRE_DECORATION_FILTER_ORDER+12;
    }

    @Override
    public boolean shouldFilter() {
//        RequestContext context = RequestContext.getCurrentContext();
//        return context.sendZuulResponse();
        return false;
    }

    @Override
    public Object run() throws ZuulException {
        //获取到用户的参数
        RequestContext context = RequestContext.getCurrentContext();
        HttpServletRequest request = context.getRequest();
        Map<String, String[]> parameterMap = request.getParameterMap();
        StringBuffer stringBuffer = new StringBuffer();
        parameterMap.forEach((key,value)->{
            // name=zhangsan,lisi,wangwu
            // name=zhangsan&name=lisi&name=wangwu
            if (!ObjectUtils.isEmpty(value)&&!"sign".equalsIgnoreCase(key)) {
                for (String s : value) {
                    stringBuffer.append(key).append("=").append(s).append("&");
                }
            }
        });
        String clientId = request.getParameter("clientId");
        //获取用户计算签名的密钥
        String signkey = cacheService.hGet(CacheConstants.CACHE_PREFIX_CLIENT + clientId, "signkey");
        //拼接密钥
        stringBuffer.append("key=").append(signkey);
        String s = stringBuffer.toString();
        System.err.println(s);
        String signcom = DigestUtils.md5DigestAsHex(s.getBytes(StandardCharsets.UTF_8));//计算出来的签名
        System.err.println("计算出来的签名"+signcom);
        String sign = request.getParameter("sign");//用户传递的签名
        if (!signcom.equalsIgnoreCase(sign)) {
            System.err.println("签名不一致，请求数据可能被篡改");
            context.setSendZuulResponse(false);
            context.setResponseBody("签名不一致，请求数据可能被篡改");
            context.addZuulResponseHeader("content-Type","text/html;charset=utf-8");
        }
        //拼接字符串
        //拼接秘钥
        //计算出签名
        //获取用户的签名
        //比较
        return null;
    }
}
